package com.tianci.de.jni;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

import com.skyworth.framework.skysdk.util.SkyJSONUtil;
import com.tianci.de.api.DEChannelSession;
import com.tianci.de.api.JsonParams;
import com.tianci.de.api.SkyDEApi;

import java.util.HashMap;

public class DEJniUtil {
    public static native void init();

    public static native void restart();

    public static native void notifyInfo(String cmd, String params);

    public static native void writeDEChannel(String sessionSerialize);

    public static native String readDEChannel(String sessionSerialize);

    private static Context mJniContext = null;

    static {
        try {
            Log.e("de", ">DEJniUtil loadLibrary success ");
            System.loadLibrary("skydeservicefree");
        } catch (Exception e) {
            Log.e("de", ">DEJniUtil loadLibrary exception = " + e.getMessage());
        }
    }

    public static void setContext(Context context) {
        mJniContext = context;
    }

    public static String getVersion() {

        int versionCode = 0;
        if (mJniContext != null) {
            try {
                PackageInfo info = mJniContext.getPackageManager().getPackageInfo(
                        mJniContext.getPackageName(), 0);
                versionCode = info.versionCode;
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }

        String versionString = String.valueOf(versionCode);
        return versionString;
    }

    private static HashMap<String, DEChannelSession> sessionMap = new HashMap<String, DEChannelSession>();
    private static HashMap<String, String> sessionCmdTargetMap = new HashMap<String, String>();

    public static void registerSession(DEChannelSession session, String fromtarget) {
        if (!sessionMap.containsKey(session.getJsonParam().getSession())) {
            sessionMap.put(session.getJsonParam().getSession(), session);
            sessionCmdTargetMap.put(session.getJsonParam().getSession(), fromtarget);
        }
        Log.i(SkyDEApi.TAG, "== registerSession, session " + SkyJSONUtil.getInstance().compile(session));
        Log.i(SkyDEApi.TAG, "== registerSession, sessionmap " + SkyJSONUtil.getInstance().compile(sessionCmdTargetMap));
    }

    public static void unregisterSession(DEChannelSession session) {
        String jsonbody = session.getJsonBody();
        JsonParams jsonparam = DEChannelSession.jsonParamFromString(jsonbody);

        Log.i(SkyDEApi.TAG, "== unregisterSession, session " + SkyJSONUtil.getInstance().compile(session));
        Log.i(SkyDEApi.TAG, "== unregisterSession, sessionmap " + SkyJSONUtil.getInstance().compile(sessionCmdTargetMap));
        if (sessionMap.containsKey(jsonparam.getSession())) {
            sessionMap.remove(jsonparam.getSession());
            sessionCmdTargetMap.remove(jsonparam.getSession());
        }
    }

    public static String getCmdTargetBySession(DEChannelSession session) {
//        Log.v(SkyDEApi.TAG, "DEJniUtil::getCmdTargetBySession");
        Log.i(SkyDEApi.TAG, "== getTargetBySession, session " + SkyJSONUtil.getInstance().compile(session));
        Log.i(SkyDEApi.TAG, "== getTargetBySession, sessionmap " + SkyJSONUtil.getInstance().compile(sessionCmdTargetMap));
        String jsonbody = session.getJsonBody();
        JsonParams jsonparam = DEChannelSession.jsonParamFromString(jsonbody);
        if (sessionCmdTargetMap.containsKey(jsonparam.getSession())) {
            return sessionCmdTargetMap.get(jsonparam.getSession());
        }
        return null;
    }

    public static boolean containsSession(DEChannelSession session) {
        String jsonbody = session.getJsonBody();
        JsonParams jsonparam = DEChannelSession.jsonParamFromString(jsonbody);

        return sessionMap.containsKey(jsonparam.getSession());
    }
}
